home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / QEDICT.C < prev    next >
C/C++ Source or Header  |  1993-06-10  |  8KB  |  384 lines

  1. /**************************************************
  2. * FILE NAME:  QEdict.c     TITLE: dictionary listing
  3. *
  4. * AUTHOR:  K. E. North II, Resource Group, Inc.
  5. *
  6. * SYNOPSIS:
  7. *    List record structure info for Q+E Lib
  8. *            
  9. ***************************************************
  10. *
  11. *    initialize (strings, counts, error_status)
  12. *    assign variables to table, view
  13. *
  14. *    initiate data base connection -
  15. *         dbConnect() to login, get cursor 
  16. *    execute SQL
  17. *         dbRequest() - prep/exec SQL statement
  18. *
  19. *    IF status <> 0 then
  20. *        while <> end of data:
  21. *             get dictionary info (dbDescribe)
  22. *            format and list columns
  23. *    ELSE
  24. *        process system error message 
  25. *        ENDIF
  26. *    terminate data base connection: dbClose()
  27. *
  28. ***************************************************/
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <conio.h>
  33. #include <string.h>
  34. #include <windows.h>
  35.  
  36. #ifndef __QEDEFS_H
  37. #include "QEdefs.h"
  38. #endif
  39.  
  40. #ifndef __QEVARBL_H
  41. #include "QEvarbl.h"
  42. #endif
  43.  
  44.  
  45. #ifndef __QEFUNCS_H
  46. #include "qefuncs.h"
  47. #endif
  48.  
  49. #ifndef __DBTYPES_H
  50. #include "dbtypes.h"
  51. #endif
  52.  
  53. #ifndef __REQUEST_H
  54. #include "request.h"
  55. #endif
  56.  
  57. #ifndef __RQOPTION_H
  58. #include "rqoption.h"
  59. #endif
  60.  
  61. #ifndef __QCOLINFO_H
  62. #include "qcolinfo.h"
  63. #endif
  64.  
  65. #ifndef __DATASRC_H
  66. #include "datasrc.h"
  67. #endif
  68.  
  69. #include "qedict.h"
  70.  
  71. #undef DEBUG
  72. #undef DEBUG1
  73.  
  74. #define RCOUNT        10
  75. #define DISPLAYLEN    20
  76.  
  77.  
  78. int        ExitStatus;
  79. int        StatusReturned;
  80.  
  81. static char    DBDictPath[80];
  82. static char    DBTable[32];
  83. static char    DBDriver[9];
  84.  
  85. char    *table;
  86. char    *dictpath;
  87. char    *driver;
  88.  
  89. COLUMNS ColInfo[MAXFIELD];
  90. COLUMNS *fi;
  91.  
  92. struct ConnectionInfo *si;
  93. struct ViewInfo *vu;
  94. struct DataSource *dsrc;
  95. struct SQLRequest *rq;
  96. struct RequestOptions *op;
  97.  
  98.  
  99.  
  100. void main (int argc, char *argv[])
  101. {
  102.  
  103. HSTMT    hstmt;
  104. POINTER    statement;
  105.  
  106.  
  107. int nColumns;
  108. int    ColPosition;
  109. int    ColCount;
  110. int    flen;        /* string length of field name */
  111. int i;                /* blank fill loop index   */
  112. int TotalLength;     /* sum of field lengths */
  113.  
  114. static char    SQLString[MAXSQL];
  115. static char    DictColName[DISPLAYLEN+1];
  116. char        *ColDataType;
  117.  
  118.                     /* allocate memory */
  119.     op = calloc(1, sizeof(struct RequestOptions));       
  120.     si = calloc(1, sizeof(struct ConnectionInfo));       
  121.     rq = calloc(1, sizeof(struct SQLRequest));
  122.     vu = calloc(1, sizeof(struct ViewInfo));
  123.     dsrc = calloc(1, sizeof(struct DataSource));
  124.  
  125.                         /* initialize strings */
  126.     memset(SQLString,'\x0',sizeof(SQLString));
  127.     memset(DBDictPath,'\x0',sizeof(DBDictPath));
  128.     memset(DBTable,'\x0',sizeof(DBTable));
  129.     memset(DBDriver,'\x0',sizeof(DBDriver));
  130.  
  131.     table         = &DBTable[0];
  132.     dictpath     = &DBDictPath[0];
  133.     driver        = &DBDriver[0];
  134.     StatusReturned = SQL_SUCCESS;
  135.     GetArgs(argc,argv[1],argv[2],argv[3],
  136.         table,dictpath,driver);
  137.  
  138.             /* make selection of database type */
  139.     strcpy (dsrc->DBDriver, driver);
  140. /*    if (strcmp,QEPDX)
  141.         {
  142.         strcpy (dsrc->DBDriver, "QEPDX");
  143.         }
  144.     if (strcmp,QEDBF)
  145. */    
  146.  
  147.             /* for DBF and PDX, same dict and datapath */
  148.     strcpy (dsrc->DBDictPath, dictpath);
  149.     strcpy (dsrc->DBDataPath, dictpath);
  150.     op->UseDirectExec    = TRUE;
  151.     op->RequiresCursor     = FALSE;
  152.     op->UseTransaction     = FALSE;
  153.     rq->CursorID = 0;    /* no cursor used for QEDBF */
  154.  
  155.     /*******************************************/
  156.     /* open data base - connect to data source */
  157.     /*******************************************/
  158.  
  159.     strcpy (si->DataPath, dsrc->DBDictPath);
  160.     strcpy (si->DictPath, dsrc->DBDictPath);
  161.  
  162.             /* may require revision for named cursors */
  163.     StatusReturned = dbConnect (si, dsrc);
  164.     if ((StatusReturned != SQL_SUCCESS))
  165.         {
  166.         printf
  167.         ("\n<QEDict> ERROR %d: Unable to connect to %s",
  168.                 si->hdbc, dsrc->DBDriver);
  169.  
  170.         exit (1);
  171.         }
  172.  
  173.                         /* specify query string */
  174.     strcpy(SQLString,"SELECT * FROM ");
  175.  
  176.     strcat(SQLString,dsrc->DBDataPath);
  177.     SQLString[strlen(SQLString)] = '\x5c';
  178.     strcat(SQLString,DBTable);
  179.  
  180.                     /************************/
  181.                     /*  prepare and execute */
  182.                     /************************/
  183.     StatusReturned = SQL_SUCCESS;
  184.  
  185.             /* may require revision for named cursors */
  186.     StatusReturned = dbRequest( rq, SQLString, si);
  187.     if ((StatusReturned != SQL_SUCCESS))
  188.         {
  189.         if (StatusReturned < 0 )
  190.             {
  191.             printf 
  192.             ("\n<QEdict> STATUS: preparing  %s\n",
  193.                      SQLString);
  194.             }
  195.         else
  196.             {
  197.             printf 
  198.             ("\n<QEdict> ERROR: preparing  %s\n", 
  199.                     SQLString);
  200.             exit (1);
  201.             }
  202.         }
  203.  
  204.         /* Loop to get the description for each    */
  205.         /* column of the table. Set the loop count */
  206.         /* from the number of columns for this     */
  207.         /* request's SQL statement                    */
  208.     nColumns = FindNumberOfColumns(rq->hstmt);
  209.  
  210.                         /************************/
  211.                         /*  get dictionary         */
  212.                         /************************/
  213.  
  214.     printf ("\nStructure for table: %s\n", DBTable);                
  215.     printf ("Column   Column Name              Type\
  216.       Width  Dec\n");
  217.  
  218.     StatusReturned = SQL_SUCCESS;            
  219.  
  220.     ColPosition = 0; 
  221.     vu->ColCount = 0;
  222.     TotalLength = 0;
  223.  
  224.     do
  225.         {
  226.                     /* point to column descriptor */
  227.         fi = &ColInfo[ColPosition];
  228.         StatusReturned = SQL_SUCCESS;
  229.  
  230.                 /* get data dictionary for 
  231.                 the current cursor ID */
  232.         StatusReturned = 
  233.             dbDescribe( ColPosition+1,
  234.                             fi,  rq );
  235.  
  236.         if ((StatusReturned != SQL_SUCCESS))
  237.             {
  238.             if (StatusReturned == ENDFILE)
  239.               {
  240.                 #ifdef DEBUG
  241.                 printf ("\n<QEDict> end of data dictionary\n");
  242.                 #endif
  243.               break;
  244.               }
  245.             else
  246.               {
  247.             printf 
  248.               ("\n<QEdict> ERROR %d: \n",
  249.                 StatusReturned);
  250.             printf 
  251.               ("\n getting data dictionary for %s\n", 
  252.                   SQLString);
  253.               exit (1);
  254.               }
  255.             };
  256.     vu->ColCount++;        /* update column count */
  257.  
  258.                         /* re-calc cols length */
  259.     TotalLength +=
  260.         ColInfo[ColPosition].Length;
  261.  
  262.     ColDataType = DecodeQEDataType(fi->DataType);
  263.     strcpy(DictColName,fi->ColName);
  264.     flen = strlen(fi->ColName);
  265.     for ( i=flen; i < DISPLAYLEN; i++ );
  266.         {
  267.         DictColName[i]='\x20';
  268.         }
  269.     DictColName[DISPLAYLEN]='\x0';
  270.     printf 
  271.     ("\n %3.0i  %20.20s  %12.12s   %5d  %2d",
  272.                         vu->ColCount,
  273.                         DictColName,
  274.                         ColDataType,
  275.                         fi->Length,
  276.                         fi->Scale);
  277.  
  278.     ColPosition++;
  279.         }
  280.     while (ColPosition < nColumns);
  281.  
  282.                     /* display summary */
  283.     printf ("\nTotal length: %d\n",TotalLength);
  284.  
  285.                         /****************/
  286.                         /*    close        */
  287.                         /****************/
  288.     StatusReturned = dbClose (si);
  289.     if ( StatusReturned )
  290.         {
  291.         printf 
  292.           ("\n<QEdict> ERROR %d: \n",
  293.             StatusReturned);
  294.         printf 
  295.           ("\n dbClose failed, disconnecting \
  296.           from: %s\n", dsrc->DBDriver);
  297.         exit (1);
  298.         }
  299. }
  300.  
  301. /**************************************************
  302. * FUNCTION NAME: GetArgs
  303. *
  304. * AUTHOR:         Ken North 
  305. *
  306. * SYNOPSIS:
  307. *
  308. *     get table name and dictionary path from 
  309. *    command line or user
  310. *
  311. ***************************************************/
  312.  
  313. void GetArgs(int argc, char *arg1, char *arg2, 
  314.         char *arg3, char *table, char *dict, 
  315.         char *driver)
  316. {
  317. static char name [80]; 
  318.  
  319.   if (argc > 1)
  320.     {
  321.     memset(name,'\x0',sizeof(name));
  322.     if (*arg1 == '?')
  323.       {
  324.       printf("\n  Usage: QEDICT table \
  325.  dictionarypath\n");
  326.       printf ("\n QEDICT    QE Lib\
  327. data definitions\n");
  328.       printf("   (Requires QELIB DLL\
  329. or QExxx.DLL)\n\n");
  330.       printf
  331.       ("    To display a data dictionary screen, \
  332.       type:\n");
  333.       printf
  334.       ("       QEDICT table dictpath driver| \
  335. MORE\n\n");
  336.       printf
  337.       ("    To list to a file, type:\n");
  338.       printf
  339.       ("       QEDICT table dictpath driver> \
  340. listfile\n\n\n");
  341.       exit(1);
  342.       }            
  343.     strcpy (name, arg1);
  344.     strupr (name);  /* translate to upper case */
  345.     strcpy(table,name) ;     /* copy name to table */
  346.     memset(name,'\x0',sizeof(name));
  347.     }
  348.   if (argc > 2)
  349.     {
  350.     strcpy (name, arg2);
  351.     strupr (name);    /* translate to upper case */
  352.     strcpy(dict,name) ;
  353.     memset(name,'\x0',sizeof(name));
  354.     }
  355.   if (argc > 3)
  356.     {
  357.     strcpy (name, arg3);
  358.     strupr (name); /* translate to upper case */
  359.     strcpy(driver,name) ;
  360.     }
  361.   if (argc < 2)
  362.       {
  363.       printf ("Table name ? ");
  364.       gets (name);
  365.       puts ("\n");
  366.       strupr (name);    /* translate to upper case */
  367.       strcpy(table,name) ;
  368.       memset(name,'\x0',sizeof(name)); /* zero name */
  369.       printf ("Dictionary pathname ? ");
  370.       gets (name);
  371.       puts ("\n");
  372.       strupr (name); 
  373.       strcpy(dict,name) ;
  374.       memset(name,'\x0',sizeof(name)); /* zero name */
  375.       printf ("Database driver ? ");
  376.       gets (name);
  377.       puts ("\n");
  378.       strupr (name); 
  379.       strcpy(driver,name) ;
  380.       }
  381. }
  382.  
  383.